package checklist.db;

import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import checklist.db.ChecklistDAO;
import checklist.entity.Check;
import checklist.entity.Checklist;

public class ChecklistFileDAO implements ChecklistDAO {
	private String filePath;
	private Document doc = null;

	public ChecklistFileDAO(String filePath) {
		this.filePath = filePath;
	}

	private void save() {
		try {
			TransformerFactory
					.newInstance()
					.newTransformer()
					.transform(new DOMSource(getDocument()),
							new StreamResult(new File(filePath)));
		} catch (Exception e) {
		}
	}

	private Document getDocument() {
		if (doc == null) {
			try {
				if (new File(filePath).exists()) {
					doc = DocumentBuilderFactory.newInstance()
							.newDocumentBuilder().parse(filePath);

				} else {
					doc = DocumentBuilderFactory.newInstance()
							.newDocumentBuilder().newDocument();
					doc.appendChild(doc.createElement("checklists"));
				}
			} catch (Exception e) {
			}
		}

		return doc;
	}

	private Node findChecklist(String game) {
		NodeList checklists = getDocument().getDocumentElement()
				.getChildNodes();
		for (int i = 0; i < checklists.getLength(); i++) {
			if (checklists.item(i).getAttributes().getNamedItem("id")
					.getNodeValue().equals(game)) {
				return checklists.item(i);
			}
		}
		return null;
	}

	public void addChecklist(String game) {
		try {
			if (findChecklist(game) == null) {
				Element newChecklist = getDocument().createElement("checklist");

				newChecklist.setAttribute("id", game);
				getDocument().getDocumentElement().appendChild(newChecklist);
				save();
			}

		} catch (Exception e) {
		}
	}

	public void addCheck(String game, String title, String description) {
		try {
			Node checklist = findChecklist(game);
			if (checklist != null) {
				Element newCheck = getDocument().createElement("check");

				newCheck.appendChild(getDocument().createElement("title"))
						.appendChild(getDocument().createTextNode(title));
				newCheck.appendChild(getDocument().createElement("description"))
						.appendChild(getDocument().createTextNode(description));
				checklist.appendChild(newCheck);
			}

			save();

		} catch (Exception e) {
		}
	}
	
	public Checklist searchChecklist(String id) {
		Checklist checklist;
		Node checklistNode = findChecklist(id);
		NodeList checkListNode;
		
		if (checklistNode == null) {
			checklist = new Checklist("");
		} else {
			checklist = new Checklist(checklistNode.getAttributes().getNamedItem("id").getNodeValue());
			checkListNode = checklistNode.getChildNodes();
			for (int i = 0; i < checkListNode.getLength(); i++) {				
				checklist.addCheck(new Check(checkListNode.item(i).getChildNodes().item(0).getTextContent(), 
						checkListNode.item(i).getChildNodes().item(1).getTextContent()));
			}
		}
		
		return checklist;
	}
}
